package com.mz.ozy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.mz.ozy.R;
import com.mz.ozy.bean.ShopSpecListBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * 标签适配器
 */
public class TagAdapter extends RecyclerView.Adapter<TagAdapter.MyViewHolder> {


    private Context mContext;
    private int selectPosition = 0;
    List<ShopSpecListBean.ShopSpecBean> beanList;
    OnItemClickListener mOnItemClickListener;
    //    private String itemSelectSpec;
    private String id;

    public TagAdapter(Context context, List<ShopSpecListBean.ShopSpecBean> beanList, OnItemClickListener mOnItemClickListener) {
        this.mContext = context;
        this.beanList = beanList;
        this.mOnItemClickListener = mOnItemClickListener;
//        this.itemSelectSpec = beanList.get(0).getName();
        this.id = id;

    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_tag, parent, false));

    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
        final ShopSpecListBean.ShopSpecBean bean = beanList.get(position);
        holder.mTagTv.setText(bean.getName());
       /* if(position == selectPosition){
             holder.mTagTv.setBackgroundResource(R.drawable.red_line_round2);
             holder.mTagTv.setTextColor(mContext.getResources().getColor(R.color.text_color_ed213f));
        }else {
            holder.mTagTv.setBackgroundResource(R.drawable.gray_line_round2);
            holder.mTagTv.setTextColor(mContext.getResources().getColor(R.color.text_color_333333));
        }*/
        if (position == selectPosition) {
//        if (itemSelectSpec.equals(bean.getName())) {
            holder.mTagTv.setBackgroundResource(R.drawable.bg_solid_73b21a_14dp);
            holder.mTagTv.setTextColor(mContext.getResources().getColor(R.color.white));
            mOnItemClickListener.OnItemClick(position);
        } else {
            holder.mTagTv.setBackgroundResource(R.drawable.bg_solid_f5f5f5_14dp);
            holder.mTagTv.setTextColor(mContext.getResources().getColor(R.color.c_1d1d1d));
        }
        //mOnItemClickListener.OnItemClick(id,bean.getValue());

        holder.mTagTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                itemSelectSpec = bean.getName();
                selectPosition = position;
                notifyDataSetChanged();
                mOnItemClickListener.OnItemClick(position);


            }
        });

    }

    public interface OnItemClickListener {
        void OnItemClick(int position);
    }


    @Override
    public int getItemCount() {
        return beanList.size();
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tagTv)
        TextView mTagTv;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
